﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace GapCC.BritishGas
{
    public class CarbonCalculation
    {
        public CarbonCalculation() { }

        private string _SchoolId;
        public string SchoolId
        {
            get { return _SchoolId; }
            set { _SchoolId = value; }
        }

        private string _ClassName;
        public string ClassName
        {
            get { return _ClassName; }
            set { _ClassName = value; }
        }

        private DateTime _Start;
        public DateTime Start
        {
            get { return _Start; }
            set { _Start = value; }
        }
        
        private DateTime _End;
        public DateTime End
        {
            get { return _End; }
            set { _End = value; }
        }

        private int _NoOfPupils;
        public int NoOfPupils
        {
            get { return _NoOfPupils; }
            set { _NoOfPupils = value; }
        }

        private float? _ElectricityKwh;
        public float? ElectricityKwh
        {
            get
            {
                return _ElectricityKwh;
            }
            set
            {
                _ElectricityKwh = value;
            }
        }

        private float? _GasKwh;
        public float? GasKwh
        {
            get { return _GasKwh; }
            set { _GasKwh = value; }
        }

        private float? _OilKwh;
        public float? OilKwh
        {
            get { return _OilKwh; }
            set { _OilKwh = value; }
        }


        private float? _OilLitres;
        public float? OilLitres
        {
            get { return _OilLitres; }
            set { _OilLitres = value; }
        }


        private float? _Butane45;
        public float? Butane45
        {
            get { return _Butane45; }
            set { _Butane45 = value; }
        }

        private float? _Butane70;
        public float? Butane70
        {
            get { return _Butane70; }
            set { _Butane70 = value; }
        }

        private float? _Butane150;
        public float? Butane150
        {
            get { return _Butane150; }
            set { _Butane150 = value; }
        }

        private float? _Propane39;
        public float? Propane39
        {
            get { return _Propane39; }
            set { _Propane39 = value; }
        }

        private float? _Propane60;
        public float? Propane60
        {
            get { return _Propane60; }
            set { _Propane60 = value; }
        }

        private float? _Propane130;
        public float? Propane130
        {
            get { return _Propane130; }
            set { _Propane130 = value; }
        }

        private float? _RenewablePercentage;
        public float? RenewablePercentage
        {
            get { return _RenewablePercentage; }
            set { _RenewablePercentage = value; }
        }

        private float? _GreenElectricity;
        public float? GreenElectricity
        {
            get { return _GreenElectricity; }
            set { _GreenElectricity = value; }
        }

        private float? _SolarPanels;
        public float? SolarPanels
        {
            get { return _SolarPanels; }
            set { _SolarPanels = value; }
        }

        private float? _WindTurbine;
        public float? WindTurbine
        {
            get { return _WindTurbine; }
            set { _WindTurbine = value; }
        }

        private float? _RenewableOther;
        public float? RenewableOther
        {
            get { return _RenewableOther; }
            set { _RenewableOther = value; }
        }

        internal CarbonFootPrint Process()
        {
            Gap.Bll.BritishGas.CarbonCalculation tmp = Gap.Bll.BritishGas.CarbonCalculation.Get(this.SchoolId, this.ClassName, this.Start, this.End);
            if (tmp == null)
            {
                tmp = Gap.Bll.BritishGas.CarbonCalculation.CreateInstance(this.SchoolId, this.ClassName, this.Start, this.End);
            }
            tmp.NoOfPupils = this.NoOfPupils;
            tmp.ElectricityKwh = this.ElectricityKwh;
            tmp.GasKwh = this.GasKwh;
            tmp.OilKwh = this.OilKwh;
            tmp.OilLitres = this.OilLitres;
            tmp.Butane45 = this.Butane45;
            tmp.Butane70 = this.Butane70;
            tmp.Butane150 = this.Butane150;
            tmp.Propane39 = this.Propane39;
            tmp.Propane60 = this.Propane60;
            tmp.Propane130 = this.Propane130;
            tmp.RenewablePercentage = this.RenewablePercentage;
            tmp.GreenElectricity = this.GreenElectricity;
            tmp.SolarPanels = this.SolarPanels;
            tmp.WindTurbine = this.WindTurbine;
            tmp.RenewableOther = this.RenewableOther;

            tmp.CalculateFootprint();

            if (!string.IsNullOrEmpty(SchoolId) && ConfigurationSettings.AppSettings["UseDB"] != "false")
            {
                tmp.SaveChanges();
            }

            return new CarbonFootPrint(tmp);

        }

        internal static CarbonCalculation Load(string schoolId, string className, DateTime start, DateTime end)
        {
            Gap.Bll.BritishGas.CarbonCalculation cc = null;
            if(ConfigurationSettings.AppSettings["UseDB"] != "false")
                cc = Gap.Bll.BritishGas.CarbonCalculation.Get(schoolId, className, start, end);

            if (cc != null)
            {
                CarbonCalculation tmp = new CarbonCalculation();
                tmp.NoOfPupils = cc.NoOfPupils;
                tmp.ElectricityKwh = cc.ElectricityKwh;
                tmp.GasKwh = cc.GasKwh;
                tmp.OilKwh = cc.OilKwh;
                tmp.OilLitres = cc.OilLitres;
                tmp.Butane45 = cc.Butane45;
                tmp.Butane70 = cc.Butane70;
                tmp.Butane150 = cc.Butane150;
                tmp.Propane39 = cc.Propane39;
                tmp.Propane60 = cc.Propane60;
                tmp.Propane130 = cc.Propane130;
                tmp.RenewablePercentage = cc.RenewablePercentage;
                tmp.GreenElectricity = cc.GreenElectricity;
                tmp.SolarPanels = cc.SolarPanels;
                tmp.WindTurbine = cc.WindTurbine;
                tmp.RenewableOther = cc.RenewableOther;
                
                return tmp;
            }
            else
            {
                return null;
            }
        }
    }
}
